package com.mdd.maduoduo;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.work.Configuration;
import androidx.work.WorkManager;

import com.mdd.maduoduo.constants.CommonConstants;
import com.mdd.maduoduo.di.ApplicationModule;
import com.mdd.maduoduo.di.component.ApplicationComponent;
import com.mdd.maduoduo.di.component.DaggerApplicationComponent;
import com.mdd.maduoduo.utils.BootReceiver;
import com.mdd.maduoduo.utils.ManufacturerUtils;
import com.mdd.maduoduo.utils.PersistentSmsReceiver;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class MyApplication extends Application {

    private ApplicationComponent applicationComponent;

    @SuppressLint("StaticFieldLeak")
    private static Context mContext;

    @SuppressLint("StaticFieldLeak")
    private static MyApplication instance;

    public static Context getContext() {
        return mContext;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    // IWXAPI 是第三方app和微信通信的openApi接口
    private IWXAPI api;

    private static final String TAG = "SmsUploaderApp";

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        instance = this;
        initializeInjector();
        api = WXAPIFactory.createWXAPI(this, CommonConstants.WX_APPID, true);
        api.registerApp(CommonConstants.WX_APPID);
        // 初始化WorkManager配置
//        initWorkManager();
//        applyManufacturerSettings();
        // 确保接收器已启用
        enableReceivers();
    }

    private void initWorkManager() {
        Configuration config = new Configuration.Builder()
                .setMinimumLoggingLevel(Log.INFO)
                .build();
        WorkManager.initialize(this, config);
    }

    private void applyManufacturerSettings() {
        new Thread(() -> {
            try {
                ManufacturerUtils.applyManufacturerSettings(this);
            } catch (Exception e) {
                Log.e(TAG, "Apply manufacturer settings failed", e);
            }
        }).start();
    }

    private void enableReceivers() {
        try {
            PackageManager pm = getPackageManager();
            ComponentName bootReceiver = new ComponentName(this, BootReceiver.class);
            ComponentName smsReceiver = new ComponentName(this, PersistentSmsReceiver.class);

            if (pm.getComponentEnabledSetting(bootReceiver)
                    != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
                pm.setComponentEnabledSetting(bootReceiver,
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
            }

            if (pm.getComponentEnabledSetting(smsReceiver)
                    != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
                pm.setComponentEnabledSetting(smsReceiver,
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
            }
        } catch (Exception e) {
            Log.e(TAG, "Enable receivers failed", e);
        }
    }

    private void initializeInjector() {
        this.applicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .build();
    }

    public ApplicationComponent getApplicationComponent() {
        return this.applicationComponent;
    }

    static {
        // 设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            return new ClassicsHeader(context).setDrawableSize(20);
        });

        // 设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            return new ClassicsFooter(context).setDrawableSize(20);
        });
    }
}
